<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>清理定时器</title>
</head>

<body>
    <button id="btn">点击清理定时器</button>
    <script>
        document.write('<br><br>清理定时器：');
        document.write('<br>一、clearTimeout(要清理的定时器名称)； 清理的是 setTimeout() 定时器。');
        document.write('<br>二、clearInterval(要清理的定时器名称)；  清理的是 setInterval() 定时器。');
        var time1 = setInterval(function () { console.log('定时器<br>'); }, 2000);
        var btn = document.getElementById('btn');
        btn.onclick = function () {
            console.log('清理 setInterval()定时器。')
            clearInterval(time1);
        }
    </script>
    <style>
        #btn{
            width:120px;
            height:50px;
            background-color:rgb(38, 219, 219);
            border: 0;
        }
    </style>
</body>

</html>